<template>
  <div class="ele-body2">
    <el-container>

      <el-container>
        <el-aside width="200px">
          <el-form style="padding-bottom: 5px"
                   :model="where"
                   label-width="60px"
                   class="ele-form-search"
                   @keyup.enter.native="reload"
                   @submit.native.prevent>
            <el-row :gutter="15">
              <el-col :lg="24" :md="24">
                <el-form-item label="日期:">
                  <el-date-picker v-model="where.date" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd'
                                  placeholder="选择日期" clearable class="ele-fluid" @change="getAll"></el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="15">
              <el-col :lg="12" :md="12">
                <el-form-item label="时间:">
                  <el-time-picker style="width: 138px"
                                  v-model="where.beginTime"
                                  :picker-options="{selectableRange: '00:00:00 - 23:59:59'}" @change="getAll" placeholder="任意时间点">
                  </el-time-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="15">
              <el-col :lg="24" :md="24">
                <el-form-item label="时间:">
                  <el-time-picker style="width: 138px"

                                  v-model="where.endTime"
                                  :picker-options="{selectableRange: '00:00:00 - 23:59:59'}" @change="getAll" placeholder="任意时间点">
                  </el-time-picker>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="15">
              <el-col :lg="24" :md="12">
                <ele-pro-table
                  ref="carTable"
                  :need-page="false"
                  :datasource="url"
                  :columns="columns"
                  :selection.sync="selection"
                  :toolbar="false"
                  :pageSize="20"
                  highlight-current-row
                  @selection-change="handleSelectionChange"
                  @row-dblclick="reset"
                  height="328px">
                  <!-- 表头工具栏 -->
                  <template slot="toolbar">
                  </template>

                  <!-- 操作列 -->
                </ele-pro-table>
                <br>
              </el-col>
            </el-row>
          </el-form>
        </el-aside>
        <el-container>
          <el-main>
            <div>
              <baidu-map style=" height: 432px;" :center="centerPoint" :zoom="10" :scroll-wheel-zoom="true"
                         class="baidu-map-view" ak="CHcUExGLPlLC6srvHoiAGDi0VWVMzNOM"
                         @ready="ready">

                <new-polyline

                  :path="polylinePath"
                  stroke-color="red"
                  :stroke-opacity="0.8"
                  :stroke-weight="8"
                ></new-polyline>

                <bm-marker :position="polylinePath[0]" :dragging="true" @click="infoWindowOpen">
                  <bm-label content="起点" :labelStyle="{ color: 'black', fontSize: '16px' }"
                            :offset="{ width: -35, height: 30 }"/>
                </bm-marker>




              </baidu-map>

            </div>
          </el-main>

        </el-container>
      </el-container>
      <el-footer>

        <el-row>

          <el-button size="mini" @click="setOrderForGoods">订单</el-button>
          <el-button  size="mini" type="primary" @click="setTAndA">温湿度</el-button>
        </el-row>
        <br><br>
        <el-row>
          <el-col :lg="24" :md="12">
            <ele-pro-table
              v-show="showTable"
              ref="OrderForGoodsTable"
              :need-page="false"
              :datasource="OrderForGoods"
              :columns="columnOrderForGoodss"
              :selection.sync="selection"
              :toolbar="false"
              :pageSize="20"
              highlight-current-row
              @row-dblclick="reset"
              height="180px">
              <!-- 表头工具栏 -->
              <template slot="toolbar">
              </template>

              <!-- 操作列 -->
            </ele-pro-table><ele-pro-table
            v-show="!showTable"
            ref="TandATable"
            :need-page="false"
            :datasource="TandA"
            :columns="columnTandAs"
            :selection.sync="selection"
            :toolbar="false"
            :pageSize="20"
            highlight-current-row
            @row-dblclick="reset"
            height="180px">
            <!-- 表头工具栏 -->
            <template slot="toolbar">
            </template>

            <!-- 操作列 -->
          </ele-pro-table>
          </el-col>
        </el-row>

      </el-footer>
    </el-container>

    <!-- <LogList
    :dataItem="current"
    :visible.sync="showLog"
    />
    <BillDetail
    :dataItem="current"
    :visible.sync="showDetail"
    /> -->

    <!-- 编辑弹窗 -->

  </div>
</template>

<script>
import {mapGetters} from "vuex";
import {
  BaiduMap,


  newPolyline,

  BmMarker,


} from 'vue-baidu-map'


//import VehicleEdit from './gpsdata-edit.vue';

//   import LogList from '../../purchase/inbound/loglist.vue';
//   import BillDetail from '../ordersync/billdetail.vue';
// import C37InfCkScBillNewEdit from './c37infckscbillnew-edit';


export default {
  name: 'transmanage_gpsdata',
  // components: {C37InfCkScBillNewEdit},
  //components: {VehicleEdit},
  computed: {

    ...mapGetters(["permission"]),
  },
  components: {
    newPolyline,


    BmMarker,
    BaiduMap,



  },
  data() {
    return {
      startPoint: {lng: 121.498369, lat: 31.227636},
      endPoint: {lng: 121.598369, lat: 31.327636},
      points:[{lng: 121.498, lat: 31.227},{lng: 121.398, lat: 31.257},{lng: 121.698, lat: 31.327},{lng: 121.598, lat: 31.727},{lng: 121.498, lat: 31.227},{lng: 122.498, lat: 31.297}],
      polylinePath: [],
      icons:[],
      TandA:[],//温湿度表
      OrderForGoods:[],//订单表
      options: [{
        value: '10',
        label: '10分钟'
      }, {
        value: '20',
        label: '20分钟'
      }, {
        value: '30',
        label: '30分钟'
      }, {
        value: '5',
        label: '5分钟'
      },],

      interval:'',
      centerPoint: {
        lng: 121.498369,
        lat: 31.227636
      }, locPoint: {},
      locName: "我的位置",

      show: false,
      // 表格数据接口
      url: '/transmanage/carinfo?page=1&limit=100000',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'carcode',
          label: '车牌',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },

      ],
      columnOrderForGoodss: [
        {

          type: 'index',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'cgoldtaxDMCodeID',
          label: '发票号',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },{
          prop: 'custcode',
          label: '客户',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },{
          prop: 'custname',
          label: '客户名称',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },{
          prop: 'custcodeSendAddr',
          label: '地址',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },{
          prop: 'sendGoodsNum',
          label: '送货数量',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },{
          prop: 'rejectGoodsNum',
          label: '拒收数量',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },{
          prop: 'statusName',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },{
          prop: 'memo2',
          label: '时间',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },

      ],
      columnTandAs: [
        {

          type: 'index',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'carNoname',
          label: '名称',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },{
          prop: 'uptDate',
          label: '时间',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },{
          prop: 'temperature1',
          label: '温度(设备实际编号)',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },{
          prop: 'temperature2',
          label: '温度(设备实际编号)',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',

        },

      ],
      showTable:true,//true显示订单表格,false显示温湿度表格
      // 表格搜索条件

      where: {},
      // 表格选中数据
      selection: [],
      carcodes: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showLog: false,
      showDetail: false,
    };
  },
  watch:{
    selection(){//选择车牌时调用

      this.getAll()

    },



  },
  mounted() {

    //this.getCarcode()
    //this.getMap()
  },
  methods: {
    /*多选框变单选框*/
    handleSelectionChange (selection) {
      this.selection = [];
      if (selection.length > 1) {
        //移除上一次选中行数据
        selection.shift();
        //修改选中图标为未选中状态
        this.$refs.carTable.clearSelection();
        //将当前选中行改为选中状态
        this.$refs.carTable.toggleRowSelection(selection[0]);
      }
      // 选中的数据
      this.selection = selection

    },
    getAll(){
      var carcodes=''
      this.TandA=[]

      for(var i=0;i<this.selection.length;i++){//拼接车牌
        if(i==0) {
          carcodes += this.selection[i].carcode
        }else{
          carcodes += ','+this.selection[i].carcode
        }
      }

      if(this.selection[0].carcode!=''||this.where.date!=''||this.where.beginTime!=''||this.where.endTime!=''){
        this.getTAndH(carcodes,this.where.date,' '+this.where.beginTime.toString().split(' ')[4],' '+this.where.endTime.toString().split(' ')[4])
        this.getOrderForGoods(carcodes,this.where.date,' '+this.where.beginTime.toString().split(' ')[4],' '+this.where.endTime.toString().split(' ')[4])
        this.getMap(this.selection[0].carcode,this.where.date,' '+this.where.beginTime.toString().split(' ')[4],' '+this.where.endTime.toString().split(' ')[4],5)



      }
    },


    setOrderForGoods(){

      this.showTable=true
    }, setTAndA(){
      this.showTable=false
    },

    async getOrderForGoods(devname,date,beginTime,endTime){//获取订单
      var res = await this.$http.get("transmanage/OrderForGoods?devname="+devname+"&beginTime="+date+beginTime+"&endTime="+date+endTime+'&page=1&limit=1000')

      if (res.status == 200) {
        this.OrderForGoods = res.data.data;
      }
    },


    async getTAndH(devnames, date, beginTime, endTime) {//获取温湿度
      var len = 0;
      var arr = devnames.split(',')

      for (var i = 0; i < arr.length; i++) {
        var res = await this.$http.get("transmanage/recordlistTempdata?devname=" + arr[i] + "&beginTime=" + date + beginTime + "&endTime=" + date + endTime + '&page=1&limit=1000')
        if (res.status == 200) {

          this.TandA = res.data.data.records


        }
        var res2 = await this.$http.get("transmanage/carinfo?carcode=" + arr[i] + '&page=1&limit=100')
        console.log(this.TandA)
        if (res2.status == 200) {
          for (var q = len; q < this.TandA.length; q++) {
            this.TandA[q].temperature1 += '(' + res2.data.data.records[0].hotdeviceid + ')'
            this.TandA[q].temperature2 += '(' + res2.data.data.records[0].hotdeviceid2 + ')'
          }
        }
        len = res.data.data.records.length
      }


    },
    async getMap(devname, date, beginTime, endTime, minuteInterval) {
      this.polylinePath = []
      //var res = await this.$http.get("transmanage/transcoordinate?devname=沪DHD035&beginTime=2024-04-01 00:00:00&&endTime=2024-07-01 23:00:00&minuteInterval=5")
      var res = await this.$http.get("transmanage/transcoordinate?devname=" + devname + "&beginTime=" + date + beginTime + "&&endTime=" + date + endTime + "&minuteInterval=" + minuteInterval)
      if (res.status == 200) {
        this.polylinePath = res.data.data.features[0].geometry.coordinates;

      }

    },

    infoWindowClose() {
      this.show = false
    },
    infoWindowOpen() {
      this.show = true
    },
    cellStyle({row, column, rowIndex, columnIndex}) {
      if (columnIndex === 9) { // 颜色列显示背景色
        return 'background-color: ' + row.memo1 + '; color: #fff;font-weight: bold;';
      } else {
        return '' + column + rowIndex;
      }
    },
    map_handler({BMap, map}) {
      this.BMap = BMap
      this.map = map
    },
    statusFormat(row, column, cellValue) {
      const daterc = row[column.property]
      if (daterc != null) {
        if (cellValue == "0") {
          return "未传报";
        } else {
          return cellValue == "20" ? "已确认" : "已传报";
        }
      }
    },
    getFromDate() {
      const timeOne = new Date()
      const year = timeOne.getFullYear()
      let month = timeOne.getMonth() - 1;
      let day = timeOne.getDate();
      //month = month < 10 ? '0' + month : month
      //day = day < 10 ? '0' + day : day
      ///const NOW_MONTHS_AGO = `${year}-${month}-${day}`
      return new Date(year, month, day);
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});

    },


    async getCarcode() {

      var res = await this.$http.get("/transmanage/carinfo?&page=1&limit=1000");
      if (res.status == 200) {
        this.carcodes = res.data.data.records

      }
    },


    /* 重置搜索 */
    reset() {
      this.where = {riqiBegin: this.getFromDate(), riqiEnd: new Date()};
      this.reload();
    },

    openLog(row) {
      console.log(row);
      this.current = row;
      this.showLog = true;
    },
    openDetail(row) {
      this.current = row;
      this.showDetail = true;
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 发票传报 */
    invoiceUpload() {
      if (this.selection.length == 0) {
        this.$message.info('请选择需要上传至阳光平台的发票。');
        return;
      }
      if (this.selection[0].status != '0') {
        this.$message.info('进项发票已传报，无需再次传报。');
        return;
      }
      this.$confirm('确定要将选中的发票上传至阳光平台吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/ygorder/yq029', {
          ddmxbhs: this.selection.map(d => d.id),
          czlx: this.where.czlx,
          ygtype: this.where.soucetype,
          uploadPsd: this.where.uploadPsd,
          uploadFp: this.where.uploadFp
        }).then(res => {
          loading.close();
          if (Number(res.data.code) === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },

    /* 发票确认 */
    invoiceConfirm() {
      if (this.selection.length == 0) {
        this.$message.info('请选择需要确认的发票。');
        return;
      }
      if (this.selection[0].status == '0') {
        this.$message.info('进项发票未传报，请先传报后再确认。');
        return;
      }
      if (this.selection[0].status == '20') {
        this.$message.info('进项发票已确认，无需再次确认。');
        return;
      }

      this.$confirm('确定要将选中的发票进行确认吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/ygorder/yq030', {
          ddmxbhs: this.selection.map(d => d.id),
          czlx: this.where.czlx,
          ygtype: this.where.soucetype,
          uploadPsd: this.where.uploadPsd,
          uploadFp: this.where.uploadFp
        }).then(res => {
          loading.close();
          if (Number(res.data.code) === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/c37infckscbillnew/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的进项发票吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/mpruchinvoice/delete/' + this.selection.map(d => d.id)).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }

}


</script>


<style scoped>
::v-deep thead .el-table-column--selection .cell {
  display: none;
}

.anchorBL {
  display: none;
}

.content {
  display: flex;
  flex-direction: column;
  margin-top: 80rpx;
  align-items: center;
  justify-content: center;
}

.bm-view {
  margin-left: 0%;
  width: 100%;
  margin-top: 10px;
  color: #999999;
  height: 90vw;
}

.el-aside::-webkit-scrollbar {

  display: none;

}
</style>
